public class BinaryTree {
    static class TreeNode {
        public char val;
        public TreeNode left;
        public TreeNode right;
        public TreeNode(char val) {
            this.val = val;
        }
    }
    public TreeNode createBinaryTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');

        A.left = B;
        A.right = C;

        B.left = D;
        B.right = E;

        C.left = F;
        C.right = G;

        E.right = H;

        return A;
    }
    /**
     * 求节点个数(遍历)
     *      1.判断是否为空
     *      2.不为空个数++
     *      3.判断左 + 右
     */
    public static int nodeSize;
    public void size(TreeNode root) {
        if(root == null) {
            return;
        }
        nodeSize++;
        size(root.left);
        size(root.right);
    }
    /**
     * 求节点个数(子问题)
     *      左 + 右 + 1
     */
    public int size2(TreeNode root) {
        if(root == null) {
            return 0;
        }
        return size2(root.left) + size2(root.right) + 1;
    }
    /**
     * 求叶子节点个数(遍历思路)
     *      1.判断根是否为空
     *      2.左右子树均为空++
     */
    public static int leafCount;
    public void getLeafNodeCount(TreeNode root) {
        if(root == null) {
            return ;
        }
        if(root.left == null && root.right == null) {
            leafCount++;
        }
        getLeafNodeCount(root.left);
        getLeafNodeCount(root.right);
    }

    /**
     * 求叶子节点个数(子问题)
     * @param root
     *  左子树叶子个数 + 右子树叶子个数
     */
    public int getLeafNodeCount2(TreeNode root) {
       if(root == null) {
           return 0;
       }
       if(root.left == null && root.right == null) {
           return 1;
       }
       return getLeafNodeCount2(root.left)
               + getLeafNodeCount2(root.right);
    }
    /**
     * 求第K层节点个数
     *      根节点的K层 = 根左的K-1+根右的k-1 = ...
     *      当k为1时,不为空,返回节点
     */
    public int getKLevelNodeCount(TreeNode root,int k) {
        if(root == null) {
            return 0;
        }
        if(k == 1) {
            return 1;
        }
        return getKLevelNodeCount(root.left,k - 1) +
        getKLevelNodeCount(root.right,k - 1);
    }
    /**
     * 获取二叉树的高度
     *      左子树高度和右子树高度最大值+1
     */
    public int getHeight(TreeNode root) {
        if(root == null) {
            return 0;
        }
        if(root.left == null && root.right == null) {
            return 1;
        }
        int leftHeight = getHeight(root.left);
        int rightHeight = getHeight(root.right);
        return Math.max(leftHeight,rightHeight) + 1;
    }
    /**
     * 检测value值是否存在
     *      1.判断头
     *      2.判断左 右
     *      3.有 返回该引用
     */
    public TreeNode find(TreeNode root,int val) {
        if(root == null) {
            return null;
        }
        if(root.val == val) {
            return root;
        }
        TreeNode leftVal = find(root.left,val);
        TreeNode rightVal = find(root.right,val);
        if(leftVal != null) {
            return leftVal;
        }
        if(rightVal != null) {
            return rightVal;
        }
        return null;
    }
    /**
     相同的树
     1.判断root是否为空
     2.判断一个为空,一个不为空情况
     3.判断值不相同情况
     4.判断左右子树情况
     */
    public boolean isSameTree(TreeNode p, TreeNode q) {
        if(p == null && q == null) {
            return true;
        }
        if(p == null && q != null || p != null && q == null) {
            return false;
        }
        if(p.val != q.val) {
            return false;
        }
        return isSameTree(p.left,q.left) && isSameTree(p.right,q.right);
    }
    /**
     另一颗树的子树
     1.判断根树与该树是否相同
     2.判断根左/根右是否和该树相同
     */
    public boolean isSubtree(TreeNode root, TreeNode subRoot) {
        if(root == null && subRoot == null) {
            return true;
        }
        if(root == null && subRoot != null || root != null && subRoot == null) {
            return false;
        }
        if(isSameTree(root,subRoot)) {
            return true;
        }
        if(isSubtree(root.left,subRoot)) {
            return true;
        }
        if(isSubtree(root.right,subRoot)) {
            return true;
        }
        return false;
    }
    /**
     翻转二叉树
     1.翻转根节点
     2.翻转根左 根右
     */
    public TreeNode invertTree(TreeNode root) {
        if(root == null) {
            return null;
        }
        if(root.left != null || root.right != null) {
            TreeNode tmp = root.left;
            root.left = root.right;
            root.right = tmp;
        }
        invertTree(root.left);
        invertTree(root.right);
        return root;
    }
}
